package com.example.download.controller;

import com.example.download.entity.Database;
import com.example.download.entity.TransferDatabase;
import com.example.download.service.SendRequestService;
import org.springframework.core.io.Resource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@RestController
@RequestMapping("/sendRequest")
public class SendRequestController {
    private final ExecutorService executorService1 = Executors.newFixedThreadPool(10); // 创建一个固定大小的线程池
    private final ExecutorService downloadService = Executors.newFixedThreadPool(10); // 创建一个固定大小的线程池

    @PostMapping("/post")
    public boolean sendRequest(@RequestBody List<TransferDatabase> transferDatabases) {
        for (TransferDatabase transferDatabase : transferDatabases) {
            executorService1.submit(() -> {
                Database database = new Database(transferDatabase);
                SendRequestService sendRequestService = new SendRequestService();
                try {
                    String s = sendRequestService.sendRequest(database);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
        }

            return true;
    }

    @GetMapping("/all")
    public boolean sendRequest() throws Exception {
        SendRequestService sendRequestService = new SendRequestService();
        List<String> strings = sendRequestService.sendRequestS();

        return true;
    }

    @PostMapping("/download")
    public boolean download(@RequestBody List<TransferDatabase> transferDatabases) throws Exception {
        SendRequestService sendRequestService = new SendRequestService(); //http://39.104.23.22:9999/kang/ table_jp014_20240620.txt

        for (TransferDatabase transferDatabase : transferDatabases) {
            executorService1.submit(() -> {
                        String s = null;
                String fileURL="http://"+transferDatabase.getIp()+":"+transferDatabase.getDownloadPort()+"/"
                        +transferDatabase.getDownloadLocation()+"/"+transferDatabase.getTableName();
                String savePath=transferDatabase.getSaveUrl();
                String name=transferDatabase.getTableName();
                        s = sendRequestService.downloadFile(fileURL, savePath, name);
                try {

                    sendRequestService.removeDuplicateData(s);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
        }

        //22
//        s =sendRequestService.downloadFile("http://121.199.68.19:9999/kang/table_jp022_20240621103454.txt","D:\\kang\\","table_jp022_20240621103454.txt");
//        sendRequestService.removeDuplicateData(s);
        //23
//        s = sendRequestService.downloadFile("http://47.97.154.245:9999/kang/table_jp023_20240621103511.txt","D:\\kang\\","table_jp023_20240621103511.txt");
//        sendRequestService.removeDuplicateData(s);
        //24
//        s = sendRequestService.downloadFile("http://39.105.164.70:9999/kang/table_jp024_20240621103525.txt","D:\\kang\\","table_jp024_20240621103525.txt");
//        sendRequestService.removeDuplicateData(s);
        //26
//        s = sendRequestService.downloadFile("http://39.105.141.129:9999/kang/table_jp026_20240621103546.txt","D:\\kang\\","table_jp026_20240621103546.txt");
//        sendRequestService.removeDuplicateData(s);
        //27
//        s = sendRequestService.downloadFile("http://139.129.37.248:9999/kang/table_jp027_20240621103602.txt","D:\\kang\\","table_jp027_20240621103602.txt");
//        sendRequestService.removeDuplicateData(s);
        //28
//        s =  sendRequestService.downloadFile("http://139.129.27.96:9999/kang/table_jp028_20240621103635.txt","D:\\kang\\","table_jp028_20240621103635.txt");
//        sendRequestService.removeDuplicateData(s);
        //29
//        s = sendRequestService.downloadFile("http://47.122.1.13:9999/kang/table_jp029_20240621103652.txt","D:\\kang\\","table_jp029_20240621103652.txt");
//        sendRequestService.removeDuplicateData(s);
        //39
//        s = sendRequestService.downloadFile("http://47.100.207.125:9999/kang/table_jp039_20240623111304.txt","D:\\kang\\","table_jp039_20240623111304.txt");
//        sendRequestService.removeDuplicateData(s);

        return true;
    }

    @GetMapping("/test")
    public boolean test() throws Exception {
        SendRequestService sendRequestService = new SendRequestService();
        sendRequestService.removeDuplicateData("");

        return true;
    }
}
